package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.store.adapter.StoreOneLotteryAdapter;
import com.meitianhui.convenitenbuy.module.store.contract.OneLotteryGGLContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.model.PayInfoBean;
import com.meitianhui.convenitenbuy.module.store.model.StoreOneLotteryBean;
import com.meitianhui.convenitenbuy.module.store.model.YYYLuckBean;
import com.meitianhui.convenitenbuy.module.store.presenter.OneLotteryGGLPresenter;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.NetworkUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.CircleImageView;
import com.meitianhui.convenitenbuy.widget.FlopView;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/24.
 * 一元抽奖之刮刮乐
 */

public class OneLotteryGGLActivity extends BaseToolbarActivity implements OneLotteryGGLContract.View {
    private String mStoreId, mStoreName, url;

    private LinearLayout relat_gridview_bg;
    private AnimationDrawable mBackAnimation;
    private ImageView ggl_shopcar;//店铺购物车
    private ImageView ggl_collect;//店铺收藏
    private TextView ggl_storename;//店铺名
    private CircleImageView ggl_title_img;//商品图片
    private TextView goods_title;//商品名
    private TextView goods_type;//商品规格
    private FlopView flopView11, flopView12, flopView13,
            flopView21, flopView22, flopView23,
            flopView31, flopView32, flopView33;
    @Inject
    OneLotteryGGLPresenter presenter;
    private StoreOneLotteryAdapter mAdapter;
    private StoreOneLotteryBean.ActivityListEntity entity;
    private PayInfoBean bean;
    private SettingStoreDialog storeDialog;
    private int lucknum;
    private YYYLuckBean ggllUckBean;
    private Dialog dialog;
    private int turnNum = 0;
    private boolean iscondet = false;//是否从店铺详情进来
    private boolean isgobackshowdialog = true;


    @Override
    protected int setView() {
        return R.layout.activity_one_lottery_ggl;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent
                .builder()
                .appComponent(App.getInstance().getAppComponent())
                .storeModule(new StoreModule(this))
                .build()
                .inject(this);
        isEnable(false);
        initId();
        initData();
        initInfo();
    }

    private void initId() {
        relat_gridview_bg = (LinearLayout) findViewById(R.id.ggl_gridview_bg);
        mBackAnimation = (AnimationDrawable) relat_gridview_bg.getBackground();
        relat_gridview_bg.post(new Runnable() {
            @Override
            public void run() {
                mBackAnimation.start();
            }
        });
        ggl_title_img = (CircleImageView) findViewById(R.id.ggl_title_img);
        goods_title = (TextView) findViewById(R.id.ggl_title_tv1);
        goods_type = (TextView) findViewById(R.id.ggl_title_tv2);
        ggl_shopcar = (ImageView) findViewById(R.id.ggl_shopcar);
        ggl_collect = (ImageView) findViewById(R.id.ggl_collect);
        ggl_storename = (TextView) findViewById(R.id.ggl_storename);
        flopView11 = (FlopView) findViewById(R.id.ggl_flopView11);
        flopView12 = (FlopView) findViewById(R.id.ggl_flopView12);
        flopView13 = (FlopView) findViewById(R.id.ggl_flopView13);
        flopView21 = (FlopView) findViewById(R.id.ggl_flopView21);
        flopView22 = (FlopView) findViewById(R.id.ggl_flopView22);
        flopView23 = (FlopView) findViewById(R.id.ggl_flopView23);
        flopView31 = (FlopView) findViewById(R.id.ggl_flopView31);
        flopView32 = (FlopView) findViewById(R.id.ggl_flopView32);
        flopView33 = (FlopView) findViewById(R.id.ggl_flopView33);
    }

    private void initInfo() {
        presenter.getGGLLuckData(entity, bean);
    }


    private void initData() {
        setTbTitle(getString(R.string.home_one_lottery_participate_type2));
        Intent intent = getIntent();
        mStoreId = intent.getStringExtra("store_id");
        mStoreName = intent.getStringExtra("store_name");
        url = intent.getStringExtra("url");
        entity = intent.getParcelableExtra(Constants.ARGUMENT);
        bean = intent.getParcelableExtra("payInfo");
        java.util.Random random = new java.util.Random();// 定义随机类
        lucknum = random.nextInt(9) + 1;// 返回[0,9)集合中的整数，注意不包括10
        setflopViewType(1);
        ggl_storename.setText(mStoreName);
        GlideUtils.setRectImage(this,url, R.mipmap.icon_default_app, R.mipmap.icon_default_app, ggl_title_img);
        goods_title.setText(entity.getTitle());
        goods_type.setText(entity.getAward_name());
        RxUtils.bindEvents(ggl_collect, this);
        RxUtils.bindEvents(ggl_shopcar, this);
        iscondet = intent.getBooleanExtra("iscondet", false);
        if (iscondet) {
            ggl_shopcar.setVisibility(View.GONE);
            ggl_collect.setVisibility(View.GONE);
        }
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.ggl_collect:
                presenter.postFavoriteStore(mStoreId);
                break;
            case R.id.ggl_shopcar:
                Intent intent = new Intent(OneLotteryGGLActivity.this, StoreDetailActivity.class);
                intent.putExtra("storeId", mStoreId);
                startActivity(intent);
                break;

        }
    }

    @Override
    protected void onRefreshData() {

    }


    @Override
    public void showError(String msg) {

    }

    @Override
    protected void clickBack() {
        showReturnDialog();
    }

    @Override
    public void onBackPressed() {
        showReturnDialog();
    }

    private void showReturnDialog() {
        if (isgobackshowdialog) {
            storeDialog = new SettingStoreDialog(this, new SettingStoreDialog.AlertDialogListener() {
                @Override
                public void onConfirmClick() {
                    storeDialog.dismiss();
                    finish();
                }

                @Override
                public void onCancelClick() {
                    storeDialog.dismiss();
                }
            });
            storeDialog.setDialogTitle("提示");
            storeDialog.setDialogContent("确定不玩了吗？");
        } else {
            finish();
        }
    }

    private void setflopViewType(int type) {
        //type 1为初始化中奖信息
        for (int i = 1; i <= 9; i++) {
            if (i == 1) {
                if (type == 1) {
                    flopView11.setAnimatorType(FlopView.ROTATION_Y);
                    flopView11.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView11.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView11.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView11);
                        }
                    });
                }
            } else if (i == 2) {
                if (type == 1) {
                    flopView12.setAnimatorType(FlopView.ROTATION_Y);
                    flopView12.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView12.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView12.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView12);
                        }
                    });
                }
            } else if (i == 3) {
                if (type == 1) {
                    flopView13.setAnimatorType(FlopView.ROTATION_Y);
                    flopView13.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView13.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView13.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView13);
                        }
                    });
                }
            } else if (i == 4) {
                if (type == 1) {
                    flopView21.setAnimatorType(FlopView.ROTATION_Y);
                    flopView21.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView21.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView21.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView21);
                        }
                    });
                }
            } else if (i == 5) {
                if (type == 1) {
                    flopView22.setAnimatorType(FlopView.ROTATION_Y);
                    flopView22.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView22.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView22.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView22);
                        }
                    });
                }
            } else if (i == 6) {
                if (type == 1) {
                    flopView23.setAnimatorType(FlopView.ROTATION_Y);
                    flopView23.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView23.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView23.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView23);
                        }
                    });
                }
            } else if (i == 7) {
                if (type == 1) {
                    flopView31.setAnimatorType(FlopView.ROTATION_Y);
                    flopView31.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView31.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView31.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView31);
                        }
                    });
                }
            } else if (i == 8) {
                if (type == 1) {
                    flopView32.setAnimatorType(FlopView.ROTATION_Y);
                    flopView32.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView32.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView32.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView32);
                        }
                    });
                }
            } else if (i == 9) {
                if (type == 1) {
                    flopView33.setAnimatorType(FlopView.ROTATION_Y);
                    flopView33.setmDuration(3000);
                    String one_imgurl = "";
                    if (i == lucknum) {
                        one_imgurl = url;
                    }
                    flopView33.setForeAndBackImage(R.mipmap.one_lottery_ggl_flop1, R.mipmap.one_lottery_ggl_flop2, one_imgurl);
                    final int finalI = i;
                    flopView33.setonFlopviewListener(new FlopView.FlopviewOnListener() {
                        @Override
                        public void onListener() {
                            setflopviewTurn(finalI, flopView33);
                        }
                    });
                }
            }
        }

    }

    private void setflopviewTurn(int whatflop, FlopView flopView) {
        if (!NetworkUtils.isNetworkConnected(this)) {
            TipUtils.showToast(App.getInstance(), "网络连接错误");
            return;
        }
        if (ggllUckBean == null) {
            TipUtils.showToast(App.getInstance(), "获取中奖信息失败,请稍后重新点击！");
            initInfo();
            return;
        }

        turnNum++;
        if (turnNum <= 3) {
            flopView.startAnimation();//执行动画
            if (lucknum == whatflop) {

                presenter.postLuckMsg(ggllUckBean);
                setallflopturn();
                setDialog(true);
                return;
            }
            if (turnNum == 3) {
                setDialog(false);
            }
        }
    }

    private void setallflopturn() {
        flopView11.startAnimation();
        flopView12.startAnimation();
        flopView13.startAnimation();

        flopView21.startAnimation();
        flopView22.startAnimation();
        flopView23.startAnimation();

        flopView31.startAnimation();
        flopView32.startAnimation();
        flopView33.startAnimation();
    }

    @Override
    public void postLuckMsgBack(boolean result, String msg) {
        if (!result) {
            TipUtils.showToast(App.getInstance(), msg);
        }
    }

    @Override
    public void getGGLLuckDataBack(boolean result, String msg, YYYLuckBean bean) {
        ggllUckBean = bean;
        if (!result) {
            TipUtils.showToast(App.getInstance(), msg);
        }
    }

    @Override
    public void postFavoriteStoreBack(boolean result, String msg) {
        if (result) {
            TipUtils.showToast(App.getInstance(), getString(R.string.home_one_lottery_yyy_favorite_success), 1);
        }
    }

    public void setDialog(final boolean isluck) {
        dialog = new Dialog(this, R.style.dialog_custom);
        dialog.setContentView(R.layout.dialog_yyy);
        if (isluck) {
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
        } else {
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
        }
        dialog.show();
        RelativeLayout dialog_img_bg = (RelativeLayout) dialog.findViewById(R.id.dia_yyy_bg_relat);
        dialog_img_bg.setVisibility(View.GONE);
        TextView dialog_tv = (TextView) dialog.findViewById(R.id.dia_yyy_tv);

        TextView dialog_chcek = (TextView) dialog.findViewById(R.id.dia_yyy_check);
        if (isluck) {
            dialog_tv.setText("恭喜你中奖了！");
            dialog_chcek.setVisibility(View.VISIBLE);
        } else {
            dialog_tv.setText("很遗憾,再接再励哦！");
            dialog_chcek.setVisibility(View.GONE);

        }
        dialog_chcek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(OneLotteryGGLActivity.this, OneLotteryParticipateActivity.class);
                startActivity(it);
                finish();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (isluck) {
                    finish();
                } else {
                    setallflopturn();
                    isgobackshowdialog = false;
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBackAnimation != null) {
            mBackAnimation.stop();
        }
    }
}
